import { setGlobalData, getGlobalData } from './util'

function getCache(key) {
  const val = getGlobalData(key)
  if (val !== undefined) return val
  
  const cache = wx.getStorageSync(key)
  if (cache === undefined || cache === '') return cache

  setGlobalData(key, cache)
  return cache
}

export const tokenKey = 'token'

export function setToken(val) {
  wx.setStorageSync(tokenKey, val)
  setGlobalData(tokenKey, val)
  return val
}

export function getToken() {
  return getCache(tokenKey)
}

export function removeToken() {
  wx.removeStorageSync(tokenKey)
  setGlobalData(tokenKey, undefined)
}

export const hardIDKey = 'HardID'

export function setHardID(val) {
  wx.setStorageSync(hardIDKey, val)
  setGlobalData(hardIDKey, val)
  return val
}

export function getHardID() {
  return getCache(hardIDKey)
}

export function removeHardID() {
  wx.removeStorageSync(hardIDKey)
  setGlobalData(hardIDKey, undefined)
}
